<?php

include('Classes/Connect.php');

if (isset($_POST['SAVE_NOTE']) && is_array($_POST['SAVE_NOTE'])) {
    foreach ($_POST['SAVE_NOTE'] as $saveNote) {
        save_note($saveNote);
    }
}

function save_note($saveNote) {
    global $dbSingleton;
    $noteExists = $dbSingleton->query('SELECT COUNT(id) AS noteExists FROM notes WHERE note_id="' . $saveNote['note_id'] . '" AND access_id=' . $saveNote['access_id']);
    if (is_object($noteExists)) {
        $noteExists->setFetchMode(PDO::FETCH_OBJ);
        if ($noteExists->noteExists > 0) {
            $dbSingleton->query('UPDATE notes SET content="' . addslashes($saveNote['note_content']) . '" WHERE note_id="' . $saveNote['note_id'] . '"');
        } else {
            $dbSingleton->query('INSERT INTO notes(note_id, content, access_id) VALUES("' . $saveNote['note_id'] . '", "' . addslashes($saveNote['note_content']) . '", ' . $saveNote['access_id'] . ');');
        }
    }
}